package dao.impl;

import com.google.common.collect.ImmutableMap;
import common.consts.Consts;
import dao.RecruitmentDao;
import domain.Recruitment;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tyler on 25/6/15.
 */
@Repository
public class RecruitmentDaoImpl extends BaseGenericDao<Recruitment, Long> implements RecruitmentDao {

    @Override
    public List<Recruitment> findByStatus(long owner, String status, int page, int rows) {
        int offset = getOffset(page, rows);

        Map<String, Object> params = newMapBuilder().put("owner", owner).put("status", status)
                .put("offset", offset).put("limit", rows).build();
        return queryList("find-by-status", params);
    }

    @Override
    public void increaseApplyQuantity(int number, long recruitmentId) {
        Map<String, Object> params = ImmutableMap.<String, Object>builder()
                .put("recruitment_id", recruitmentId)
                .put("quantity", number)
                .build();
        update("increase-work-apply-quantity", params);
    }

    @Override
    public List<Recruitment> filterByConditionFromGivenIds(List<Long> ids, Map<String, Object> condition) {
        condition.put("ids", ids);
        return queryList("filter-by-condition-from-given-ids", condition);
    }

    @Override
    public Map<String, Object> getWithDistance(long id, double longitude, double latitude) {
        Map<String, Object> params = newMapBuilder()
                .put("id", id)
                .put("longitude", longitude)
                .put("latitude", latitude)
                .build();
        return queryOne("get-with-distance", params);
    }

    @Override
    public List<Map<String, Object>> findByConditionAndUserGeo(Map<String, Object> condition, Double userLng, Double userLat, int page) {
        int offset = getOffset(page, Consts.DEFAULT_ROWS);
        condition.put("longitude", userLng);
        condition.put("latitude", userLat);
        condition.put("offset", offset);
        condition.put("limit", Consts.DEFAULT_ROWS);

        return queryList("find-by-condition-and-user-geo", condition);
    }

    @Override
    public void updateStatus(Long recruitmentId, Map<String, Object> status) {
        Map<String, Object> params = new HashMap<>();
        params.put("id", recruitmentId);
        params.putAll(status);
        update("update-status", params);
    }

    @Override
    public void updateRecruitQuantity(Long id, int recruited) {
        Map<String, Object> params = newMapBuilder().put("id", id).put("recruit_quantity", recruited).build();
        update("update-recruit-quantity", params);
    }
}
